﻿#pragma once
// Copyright (c) lanyeo 2020-2021
// Licenced under MIT license. See https://opensource.org/licenses/MIT for details.
// code style
// 1. use forward declaration (尽可能使用前置声明)
// 2. include file sequence [other project>c>c++>third>self project]
// 

#include "rpc.pb.h"
#include "service_token.h"

template <typename T>
inline std::string SerializeAsString(const T& a)
{
    return a.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const int32_t& a)
{
    SerializeIntWrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const std::string& a)
{
    return a;
}

template <>
inline std::string SerializeAsString(const uint32_t& a)
{
    SerializeUIntWrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const int64_t& a)
{
    SerializeInt64Wrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const uint64_t& a)
{
    SerializeUInt64Wrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const double& a)
{
    SerializeDoubleWrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const float& a)
{
    SerializeFloatWrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

template <>
inline std::string SerializeAsString(const bool& a)
{
    SerializeBoolWrapper wrapper;
    wrapper.set_value(a);
    return wrapper.SerializeAsString();
}

//-------------------解析---------------------

template <typename T>
T ParseFromString(const std::string& from)
{
    T t;
    t.ParseFromString(from);
    return t;
}

template <>
inline std::string ParseFromString(const std::string& from)
{
    return from;
}

template <>
inline int32_t ParseFromString(const std::string& from)
{
    SerializeIntWrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}

template <>
inline uint32_t ParseFromString(const std::string& from)
{
    SerializeUIntWrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}

template <>
inline int64_t ParseFromString(const std::string& from)
{
    SerializeInt64Wrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}

template <>
inline uint64_t ParseFromString(const std::string& from)
{
    SerializeUInt64Wrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}

template <>
inline double ParseFromString(const std::string& from)
{
    SerializeDoubleWrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}

template <>
inline float ParseFromString(const std::string& from)
{
    SerializeFloatWrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}

template <>
inline bool ParseFromString(const std::string& from)
{
    SerializeBoolWrapper wrapper;
    wrapper.ParseFromString(from);
    return wrapper.value();
}
